import { store } from "@/store";
import { usePermissionStoreHook } from "@/store/modules/permission.store";
import { useDictStoreHook } from "@/store/modules/dict.store";

import AuthAPI from "@/api/auth.api";
import UserAPI from "@/api/system/user.api";

import { setAccessToken, setRefreshToken, getRefreshToken, clearToken } from "@/utils/auth";


export const useUserStore = defineStore( "user", () => {
    const userInfo = useStorage( "userInfo", {} );

    /**
     * 登录
     */
    function login ( LoginFormData ) {
        return new Promise( ( resolve, reject ) => {
            AuthAPI.login( LoginFormData )
                .then( ( reponseData ) => {
                    // console.log(data)
                    const { accessToken, refreshToken } = reponseData.data.data;
                    setAccessToken( accessToken );
                    setRefreshToken( refreshToken );
                    resolve();
                } )
                .catch( ( error ) => {
                    reject( error );
                } );
        } );

    }

    /**获取用户信息 */
    function getUserInfo () {
        return new Promise( ( resolve, reject ) => {
            UserAPI.getInfo()
                .then( ( data ) => {
                    if ( !data ) {
                        reject( "Verification failed, please Login again." );
                        return;

                    }

                    Object.assign( userInfo.value, { ...data } );
                    resolve( data );
                } )
                .catch( ( error ) => {
                    reject( error );
                } );
        } );
    }

    /**登出 */
    function logout () {
        return new Promise( ( resolve, reject ) => {
            AuthAPI.logout()
                .then( () => {
                    clearSessionAndCache();
                    resolve();
                } )
                .catch( ( error ) => {
                    reject( error );
                } );
        } );
    }

    /**刷新token */
    function refreshToken () {
        const refreshToken = getRefreshToken();
        return new Promise( ( resolve, reject ) => {
            AuthAPI.refreshToken( refreshToken )
                .then( ( data ) => {
                    const { accessToken, refreshToken } = data;
                    setAccessToken( accessToken );
                    setRefreshToken( refreshToken );
                    resolve();
                } )
                .catch( ( error ) => {
                    console.log( "refreshToken 刷新失败", error );
                    reject();
                } );
        } );
    }

    /**清除用户会话缓存 */
    function clearSessionAndCache () {
        return new Promise( ( resolve ) => {
            clearToken();
            usePermissionStoreHook().resetRouter();
            useDictStoreHook().clearDictCache();
            userInfo.value = {};
            resolve();
        } );
    }

    return {
        userInfo,
        getUserInfo,
        login,
        logout, clearSessionAndCache,
        refreshToken,
    };
} );

export function useUserStoreHook () {
    return useUserStore( store );
}